package com.itserver.service.article.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itserver.mapper.art.ArticleInfoMapper;
import com.itserver.pojo.PageResult;
import com.itserver.pojo.article.ArticleInfo;
import com.itserver.pojo.article.QueryArticleInfo;
import com.itserver.service.article.ArticleInfoService;
import com.itserver.utils.RequestHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ArticleInfoServiceImpl implements ArticleInfoService{

    @Autowired
    private ArticleInfoMapper articleInfoMapper;
    @Override
    public PageResult<ArticleInfo> list(QueryArticleInfo params) {
        PageHelper.startPage(params.getPage(), params.getPageSize());
        List<ArticleInfo> rows = articleInfoMapper.list(params);
        Page<ArticleInfo> page = (Page<ArticleInfo>) rows;
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public void delete(List<Integer> ids) {
        articleInfoMapper.delete(ids);
    }

    @Override
    public void add(ArticleInfo articleInfo) {
        Integer authId = RequestHeader.getUserId();
        articleInfo.setAuthorId(authId);
        // 设置状态 未发布
        articleInfo.setState(0);
        articleInfoMapper.add(articleInfo);
    }

    @Transactional
    @Override
    public void publish(List<Integer> ids) {
        articleInfoMapper.publish(ids);
    }

    @Override
    public void update(ArticleInfo articleInfo) {
        articleInfoMapper.update(articleInfo);
    }

    @Override
    public ArticleInfo getById(Integer id) {
        return articleInfoMapper.getById(id);
    }
}
